Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Editor component #1851

Merged
merged 6 commits into from
Oct 2, 2023
Merged

Add Editor component #1851

merged 6 commits into from
Oct 2, 2023

Conversation

masenf
Copy link
Collaborator

@masenf masenf commented Sep 21, 2023

Wrap the excellent SunEditor WYSIWYG HTML editor / suneditor-react component.

Based on work in #1060 originally submitted by @iron3oxide.

This fixes the event triggers and button_list, which were the two outstanding items on that PR. It also updates everything to use the latest Reflex features.

Closes #995

Sample

Screen.Recording.2023-09-21.at.2.54.43.PM.mov
import httpx

import reflex as rx


class State(rx.State):
    loading: bool = False
    content: str = ""
    
    def on_change(self, c):
        self.content = c
        print(f"Content: {c}")

    def on_focus(self):
        print("Focused!")

    def on_blur(self, c):
        print(f"Blurred! content len={len(c)}")

    def toggle_code_view(self, c):
        print(f"Code view toggled! {c}")

    def toggle_full_screen(self, c):
        print(f"Full screen toggled! {c}")

    def load_content(self):
        self.loading = True
        yield
        self.content = httpx.get("http://example.com/").text
        self.loading = False


def index() -> rx.Component:
    return rx.fragment(
        rx.color_mode_button(rx.color_mode_icon(), float="right"),
        rx.vstack(
            rx.editor(
                set_contents=State.content,
                set_options=rx.EditorOptions(
                    button_list=[
                        ["undo", "redo"],
                        ["font", "fontSize", "formatBlock"],
                        ["fontColor", "hiliteColor"],
                        ["bold", "underline", "italic", "strike", "subscript", "superscript"],
                        ["removeFormat"],
                        "/",
                        ["outdent", "indent"],
                        ["align", "horizontalRule", "list", "table"],
                        ["link"],
                        ["fullScreen", "showBlocks", "codeView"],
                        ["preview", "print"],
                    ]
                ),
                width="75vw",
                height="50vh",
                on_change=State.on_change,
                on_focus=State.on_focus,
                on_blur=State.on_blur,
                toggle_code_view=State.toggle_code_view,
                toggle_full_screen=State.toggle_full_screen,
            ),
            rx.cond(
                State.loading,
                rx.spinner(),
                rx.button("Load External Content", on_click=State.load_content),
            ),
        ),
    )


app = rx.App()
app.add_page(index)
app.compile()

@masenf masenf mentioned this pull request Sep 21, 2023
6 tasks
Copy link
Contributor

@picklelo picklelo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for the delay, looks great!

@picklelo picklelo merged commit 0a19669 into main Oct 2, 2023
36 checks passed
@picklelo picklelo deleted the masenf/sun-editor branch October 9, 2023 21:07
Lendemor pushed a commit that referenced this pull request Oct 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add WYSIWYG text editor component
2 participants